package net.qhzw.admin.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.regexp.internal.RE;
import net.qhzw.admin.entity.SysArea;
import net.qhzw.admin.entity.SysMechanism;
import net.qhzw.admin.mapper.SysAreaMapper;
import net.qhzw.admin.mapper.SysMechanismMapper;
import net.qhzw.admin.param.MechanismParam;
import net.qhzw.admin.pojo.SysMechanismVo;
import net.qhzw.admin.query.MechanismQuery;
import net.qhzw.admin.service.SysMechanismService;
import net.qhzw.common.model.JsonResult;
import net.qhzw.common.util.StringUtils;
import net.qhzw.common.util.UUIDUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


@Service
public class SysMechanismServiceImpl implements SysMechanismService {

    @Autowired
    private SysMechanismMapper mechanismMapper;

    @Autowired
    private SysAreaMapper areaMapper;

    /****
     * 根据id获取当前机构信息
     * @param id
     * @return
     */
    @Override
    public JsonResult getMechanism(String id) {
        SysMechanismVo sysMechanism = mechanismMapper.selectMechanism(id);
        String cityId = sysMechanism.getCityId();
        List<String> list = JSONObject.parseArray(cityId, String.class);
        if (list != null && !list.isEmpty()) {
            List<Long> cityIdList = new ArrayList<>();
            for (String s : list) {
                cityIdList.add(Long.parseLong(s));
            }
            List<SysArea> sysAreaList = areaMapper.selectByPrimaryKeyList(cityIdList);
            sysMechanism.setSysAreaList(sysAreaList);
        }
        sysMechanism.setCityIds(list);
        return JsonResult.success(sysMechanism);
    }


    /***
     * 获取机构信息列表
     * @param query
     * @return
     */
    @Override
    public JsonResult getMechanismList(MechanismQuery query) {
        Map<String, Object> resultMap = new HashMap<>();
        PageHelper.startPage(query.getPage(), query.getLimit());
        List<SysMechanismVo> mechanismVoList = mechanismMapper.selectMechanismList(query);
        if (mechanismVoList != null && !mechanismVoList.isEmpty()) {
            for (SysMechanismVo mechanismVo : mechanismVoList) {
                String cityId = mechanismVo.getCityId();
                List<String> list = JSONObject.parseArray(cityId, String.class);
                if (list != null && !list.isEmpty()) {
                    List<Long> cityIdList = new ArrayList<>();
                    for (String s : list) {
                        cityIdList.add(Long.parseLong(s));
                    }
                    List<SysArea> sysAreaList = areaMapper.selectByPrimaryKeyList(cityIdList);
                    mechanismVo.setSysAreaList(sysAreaList);
                }
                mechanismVo.setCityIds(list);
            }
        }
        PageInfo<SysMechanismVo> pageInfo = new PageInfo<>(mechanismVoList);
        resultMap.put("total", pageInfo.getTotal());
        resultMap.put("list", pageInfo.getList());
        return JsonResult.success(resultMap);
    }


    /***
     * 添加|修改机构信息
     * @param param
     * @return
     */
    @Override
    public JsonResult editMechanism(MechanismParam param) {
        if (StringUtils.isEmpty(param.getId())) {
            //添加
            SysMechanism sysMechanism = new SysMechanism();
            BeanUtils.copyProperties(param, sysMechanism);
            sysMechanism.setCreateTime(new Date());
            sysMechanism.setId(UUIDUtil.getUUid());
            if (param.getCityIds() != null && param.getCityIds().length > 0) {
                String[] cityIds = param.getCityIds();
                sysMechanism.setCityId(JSON.toJSONString(cityIds));
            }
            mechanismMapper.insertSelective(sysMechanism);
            return JsonResult.success();
        }
        SysMechanism sysMechanism = new SysMechanism();
        BeanUtils.copyProperties(param, sysMechanism);
        if (param.getCityIds() != null && param.getCityIds().length > 0) {
            String[] cityIds = param.getCityIds();
            sysMechanism.setCityId(JSON.toJSONString(cityIds));
        }
        mechanismMapper.updateByPrimaryKeySelective(sysMechanism);
        return JsonResult.success();
    }


    /***
     * 删除机构
     * @param id
     * @return
     */
    @Override
    public JsonResult deleteMechanism(String id) {
        int row = mechanismMapper.deleteByPrimaryKey(id);
        return JsonResult.success();
    }


    /***
     * 判断修改数据库返回值 返回
     * @param row
     * @param msg
     * @return
     */
    private JsonResult result(int row, String msg) {
        if (row > 0) {
            return JsonResult.success(null);
        }
        return JsonResult.error(null, msg);
    }
}
